package com.cornergj.back.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.model.Admin;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.FrontImgCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.ServiceContentCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.FrontImg;
import com.cornergj.model.ServiceContent;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.FrontImgService;
import com.cornergj.service.ServiceContentService;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/service")
public class ServiceContentController {
	
	@Autowired
	private ServiceContentService serviceContentService;
	
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, ServiceContent bean, Pagination pagination) {
		//查询条件封装
		ServiceContentCriteria criteria = new ServiceContentCriteria();
		//分页查询
		Pagination list = serviceContentService.selectPage(criteria, pagination);
		
		request.setAttribute("pagination", list);
		request.setAttribute("bean", bean);
		return "servicecontent/list";
	}
	
	/**
	 * 编辑跳转
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(HttpServletRequest request, int serviceId) {
		ServiceContent serviceContent = serviceContentService.selectOne(serviceId);
		request.setAttribute("bean", serviceContent);
		return "servicecontent/edit";
	}
	/**
	 * 添加跳转
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(HttpServletRequest request) {
		return "servicecontent/add";
	}
	/**
	 * 更新
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(HttpServletRequest request,ServiceContent serviceContent,@RequestParam(value = "labelfile", required = false) MultipartFile labelfile) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		ServiceContent getserviceContent=serviceContentService.selectOne(serviceContent.getId());
		try{
			if(labelfile!=null){
				if(FileUtils.deleteFile(getserviceContent.getImgKey())){
					byte[] multipartbackfilebytes=labelfile.getBytes();
					String backfileimg_key=FileUtils.uploadFileBybyte(multipartbackfilebytes);
					String backfileimg_url=FileUtils.getFileUrls(backfileimg_key);
					serviceContent.setImgKey(backfileimg_key);
					serviceContent.setImgUrl(backfileimg_url);
				}else{
					jsonResult.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
					return jsonResult;
				}
			}
			serviceContentService.update(serviceContent);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
		}
		return jsonResult;
	}
	/**
	 * 添加
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult save(HttpServletRequest request,ServiceContent serviceContent,@RequestParam(value = "labelfile", required = false) MultipartFile labelfile) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			/*
			 * 上传(以字节流的形式上传)
			 */
			byte[] multipartbackfilebytes=labelfile.getBytes();
			String labelfileimg_key=FileUtils.uploadFileBybyte(multipartbackfilebytes);
			String labelfileimg_url=FileUtils.getFileUrls(labelfileimg_key);
			serviceContent.setImgKey(labelfileimg_key);
			serviceContent.setImgUrl(labelfileimg_url);
			serviceContent.setIshot(0);
			serviceContentService.save(serviceContent);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.UPLOAD_FILE_FAIL_ERROR);
		}
		return jsonResult;
	}
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult delete(HttpServletRequest request,int serviceid) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			ServiceContent getserviceContent=serviceContentService.selectOne(serviceid);
			if(FileUtils.deleteFile(getserviceContent.getImgKey())){
				serviceContentService.delete(serviceid);
			}else{
				jsonResult.addErrorCode(ErrorCode.DELETE_FILE_FAIL_ERROR);
			}
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.DELETE_FILE_FAIL_ERROR);
		}
		return jsonResult;
	}
	/**
	 * 是否热销
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/ishot", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult ishot(HttpServletRequest request,String serviceId,String ishot) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		ServiceContent getserviceContent=serviceContentService.selectOne(Integer.valueOf(serviceId));
		try{
			int sishot=0;
			if("true".equals(ishot)){
				sishot=1;
			}else if("false".equals(ishot)){
				sishot=0;
			}
			getserviceContent.setIshot(sishot);
			serviceContentService.update(getserviceContent);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.SYS_ERR);
		}
		return jsonResult;
	}
}
